package ink.carnation.imtalk.controller.friend;

import ink.carnation.imtalk.annotation.IPLimit;
import ink.carnation.imtalk.annotation.Limiter;
import ink.carnation.imtalk.annotation.UserLoginToken;
import ink.carnation.imtalk.model.IMResult;
import ink.carnation.imtalk.model.po.UserPo;
import ink.carnation.imtalk.model.vo.FriendVO;
import ink.carnation.imtalk.service.FriendService;
import ink.carnation.imtalk.service.UserService;
import ink.carnation.imtalk.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api("好友系统")
@RequestMapping("/friends")
public class FriendController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private UserService userService;

    @GetMapping
    @ApiOperation("获得当前用户的所有好友")
    @Limiter(LimitNum = 20)
    @IPLimit(count = 10, time = 60)
    @UserLoginToken
    public IMResult getFriendList(HttpServletRequest request) {
        int userId = friendService.getUserIdByRequest(request);
        List<FriendVO> friendList = friendService.getFriends(userId);
        if (!CollectionUtils.isEmpty(friendList)) {
            return IMResult.success(friendList);
        }
        return IMResult.success("好友为空");
    }

    @PostMapping("/send")
    @ApiOperation("根据好友friendId添加好友")
    @UserLoginToken
    @Limiter(LimitNum = 5)
    @IPLimit(count = 5, time = 60)
    public IMResult addFriend(HttpServletRequest request, @RequestParam("friendId") Integer friendId) {
        Integer userId = friendService.getUserIdByRequest(request);
        int i = friendService.addFriend(userId, friendId);
        if (i > 0) {
            return IMResult.success("好友申请已发送");
        } else if (i == -1) {
            return IMResult.failure("好友已存在，添加失败");
        } else {
            return IMResult.failure("好友添加失败，请重试");
        }
    }

    @PostMapping("/confirm")
    @ApiOperation("确认添加好友请求，传入friendId")
    @UserLoginToken
    @Limiter(LimitNum = 5)
    @IPLimit(count = 5, time = 60)
    public IMResult confirmAddFriend(HttpServletRequest request, @RequestParam("friendId") Integer friendId) {
        Integer userId = friendService.getUserIdByRequest(request);
        int i = friendService.confirmAddFriend(userId, friendId);
        if (i == -1) {
            return IMResult.failure("好友已存在");
        } else if (i > 0) {
            return IMResult.success();
        } else {
            return IMResult.failure("添加失败，请重试");
        }
    }

    @DeleteMapping("/{friendId}")
    @UserLoginToken
    @ApiOperation("根据id删除好友")
    @Limiter(LimitNum = 5)
    @IPLimit(count = 5, time = 60)
    public IMResult deleteFriend(HttpServletRequest request, @PathVariable("friendId") Integer friendId) {
        Integer userId = friendService.getUserIdByRequest(request);
        if (friendService.deleteFriend(userId, friendId)) {
            return IMResult.success();
        } else {
            return IMResult.failure("删除好友失败");
        }
    }

    @GetMapping("/getFriendByNicknameOrId")
    @ApiOperation("flag=1代表根据friendId进行查询，flag=0代表根据nickname进行模糊查询，" +
            "url后面需要加上查询参数param，如/getFriendByNicknameOrId?flag=1&&param=10010")
    @UserLoginToken
    @Limiter(LimitNum = 5)
    @IPLimit(count = 5, time = 60)
    public IMResult getFriendByNickNameOrId(HttpServletRequest request,
                                            @RequestParam(value = "param", required = true) String param,
                                            @RequestParam(value = "flag", required = true) Integer flag) {
        if (flag == 1) {
            Integer friendId = Integer.valueOf(param);
            UserPo friend = friendService.getFriendByFriendId(request, friendId);
            if (friend == null) {
                return IMResult.failure("未找到好友，请确认您的好友id");
            }
            FriendVO friendVO = new FriendVO();
            BeanUtils.copyProperties(friend, friendVO);
            friendVO.setId(friend.getId());
            return IMResult.success(friendVO);
        } else if (flag == 0) {
            List<UserPo> userList = friendService.getFriendByNickName(request, param);
            if (CollectionUtils.isEmpty(userList)) {
                return IMResult.failure("未找到好友，请确认您的好友昵称");
            }
            List<FriendVO> friendList = new ArrayList<>(userList.size());
            FriendVO friendVO = new FriendVO();
            for (UserPo userPo : userList) {
                BeanUtils.copyProperties(userPo, friendVO);
                friendVO.setId(userPo.getId());
                friendList.add(friendVO);
            }
            return IMResult.success(friendList);
        } else {
            return IMResult.failure("flag错误，请重新查询");
        }
    }
}
